#include <iostream>
#include <dlib/image_io.h>
#include "ConfigParser.h"
#include "FaceRecognizer.h"

int main() {
    // 加载配置文件
    ConfigParser config;
    if (!config.load("/home/hhy212306170/practice/dlib/s4/config.txt")) {
        std::cerr << "配置文件加载失败" << std::endl;
        return -1;
    }
    
    // 打印配置文件内容（调试用）
    if (config.get("debug") == "true") {
        config.printAll();
    }
    
    // 创建人脸识别器
    try {
        FaceRecognizer face_recognizer(config.getConfigMap());
        
        if (!face_recognizer.initialize()) {
            std::cerr << "人脸识别器初始化失败" << std::endl;
            return -1;
        }
        
        // 打印人脸库信息
        face_recognizer.printFaceLibInfo();
        
        // 测试1：使用配置文件中指定的已知人脸图片
        std::string known_image_path = config.get("jpg_path");
        if (known_image_path.empty()) {
            std::cerr << "配置文件中未指定已知人脸图片路径 (jpg_path)" << std::endl;
            return -1;
        }
        
        std::cout << "\n[测试1] 使用已知人脸图片进行测试: " << known_image_path << std::endl;
        dlib::matrix<dlib::rgb_pixel> known_img;
        
        try {
            dlib::load_image(known_img, known_image_path);
        } catch (dlib::image_load_error& e) {
            std::cerr << "图片加载失败: " << e.what() << std::endl;
            return -1;
        }
        
        // 检测人脸
        auto faces = face_recognizer.detectFaces(known_img);
        std::cout << "检测到 " << faces.size() << " 张人脸" << std::endl;
        
        if (!faces.empty()) {
            // 对齐并识别人脸
            for (size_t i = 0; i < faces.size(); ++i) {
                auto face_chip = face_recognizer.alignFace(known_img, faces[i]);
                std::string result = face_recognizer.recognizeFace(face_chip);
                std::cout << "人脸 " << i+1 << ": " << result << std::endl;
                
                // 添加新样本到人脸库（可选）
                if (config.get("add_to_facelib") == "true" && result == "陌生人") {
                    std::string name;
                    std::cout << "请输入姓名添加到人脸库: ";
                    std::cin >> name;
                    face_recognizer.saveFaceDescriptor(name, face_chip);
                    std::cout << "已添加到人脸库" << std::endl;
                }
            }
        }
        
        // 测试2：使用配置文件中指定的陌生人照片
        std::string stranger_path = config.get("stranger_path");
        if (stranger_path.empty()) {
            std::cerr << "配置文件中未指定陌生人图片路径 (stranger_path)" << std::endl;
            return -1;
        }
        
        std::cout << "\n[测试2] 使用陌生人照片进行测试: " << stranger_path << std::endl;
        dlib::matrix<dlib::rgb_pixel> stranger_img;
        
        try {
            dlib::load_image(stranger_img, stranger_path);
        } catch (dlib::image_load_error& e) {
            std::cerr << "陌生人图片加载失败: " << e.what() << std::endl;
            return -1;
        }
        
        // 检测人脸
        faces = face_recognizer.detectFaces(stranger_img);
        std::cout << "检测到 " << faces.size() << " 张人脸" << std::endl;
        
        if (!faces.empty()) {
            // 对齐并识别人脸
            for (size_t i = 0; i < faces.size(); ++i) {
                auto face_chip = face_recognizer.alignFace(stranger_img, faces[i]);
                std::string result = face_recognizer.recognizeFace(face_chip);
                std::cout << "人脸 " << i+1 << ": " << result << std::endl;
            }
        }
        
        // 测试3：比较两张人脸照片
        if (config.get("compare_test") == "true") {
            std::cout << "\n[测试3] 人脸比对测试" << std::endl;
            
            // 对齐两张人脸
            auto face_chip1 = face_recognizer.alignFace(known_img, faces[0]);
            auto face_chip2 = face_recognizer.alignFace(stranger_img, faces[0]);
            
            double distance = face_recognizer.compareFaces(face_chip1, face_chip2);
            std::cout << "欧氏距离: " << distance << std::endl;
            
            if (distance < std::stod(config.get("threshold"))) {
                std::cout << "判定为同一个人" << std::endl;
            } else {
                std::cout << "判定为不同人" << std::endl;
            }
        }
        
    } catch (const std::exception& e) {
        std::cerr << "错误: " << e.what() << std::endl;
        return -1;
    }
    
    return 0;
}